package com.zhangxu.microservice.common.utils;

import com.zhangxu.microservice.common.exception.AssertException;

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;

public class AssertUtils {

    private AssertUtils() {
    }

    public static <T> void isEqual(T t1, T t2, String message) {
        if (t1 != null && t2 != null) {
            if (!t1.equals(t2)) {
                throw new AssertException(message);
            }
        } else {
            throw new AssertException(message);
        }
    }

    public static <T> void isNotEqual(T t1, T t2, String message) {
        if (t1 != null && t2 != null) {
            if (t1.equals(t2)) {
                throw new AssertException(message);
            }
        } else {
            throw new AssertException(message);
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new AssertException(message);
        }
    }

    public static void isFalse(boolean expression, String message) {
        if (expression) {
            throw new AssertException(message);
        }
    }

    public static void isTrue(boolean expression, Supplier<String> messageSupplier) {
        if (!expression) {
            throw new AssertException(nullSafeGet(messageSupplier));
        }
    }

    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new AssertException(message);
        }
    }

    public static void isNull(Object object, Supplier<String> messageSupplier) {
        if (object != null) {
            throw new AssertException(nullSafeGet(messageSupplier));
        }
    }

    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new AssertException(message);
        }
    }

    public static void notNull(Object object, Supplier<String> messageSupplier) {
        if (object == null) {
            throw new AssertException(nullSafeGet(messageSupplier));
        }
    }

    public static void hasLength(String text, String message) {
        if (null == text || text.isEmpty()) {
            throw new AssertException(message);
        }
    }

    public static void hasLength(String text, Supplier<String> messageSupplier) {
        if (null == text || text.isEmpty()) {
            throw new AssertException(nullSafeGet(messageSupplier));
        }
    }

    public static void notEmpty(Object[] array, String message) {
        if (array == null || array.length == 0) {
            throw new AssertException(message);
        }
    }

    public static void notEmpty(Object[] array, Supplier<String> messageSupplier) {
        if (array == null || array.length == 0) {
            throw new AssertException(nullSafeGet(messageSupplier));
        }
    }

    public static void notEmpty(Collection<?> collection, String message) {
        if (collection == null || collection.isEmpty()) {
            throw new AssertException(message);
        }
    }

    public static void notEmpty(Collection<?> collection, Supplier<String> messageSupplier) {
        if (collection == null || collection.isEmpty()) {
            throw new AssertException(nullSafeGet(messageSupplier));
        }
    }

    public static void notEmpty(Map<?, ?> map, String message) {
        if (null == map || map.isEmpty()) {
            throw new AssertException(message);
        }
    }

    public static void notEmpty(Map<?, ?> map, Supplier<String> messageSupplier) {
        if (null == map || map.isEmpty()) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    private static String nullSafeGet(Supplier<String> messageSupplier) {
        return messageSupplier != null ? messageSupplier.get() : null;
    }
}
